//
//  DYCreateSignatureTextCell.m
//  DaYing
//
//  Created by DaYing Team on 2023.
//  Copyright © 2023 DaYing Team. All rights reserved.
//

#import "DYCreateSignatureTextCell.h"
#import <objc/runtime.h>

@implementation DYCreateSignatureTextCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    _textField.delegate = self;

    Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
    UILabel *minPlaceholderLabel = object_getIvar(_textField, ivar);
    minPlaceholderLabel.textColor = UIColorWithHex(0x999999);
    minPlaceholderLabel.font = [UIFont systemFontOfSize:16];
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];//创建toolbar
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(hiddenKeyboard)];//创建系统item按钮，并绑定一个方法
    [toolbar setItems:@[item]];//将item添加到toolbar上
    [_textField setInputAccessoryView:toolbar];//将toolbar添加到InputAccessoryView上
}

- (void)hiddenKeyboard {
    [_textField resignFirstResponder];
}

- (void)setData:(id)data delegate:(id)delegate {
    
    _delegate = delegate;
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        if ([data[@"data"] isKindOfClass:[DYCreateSignatureTextCellModel class]]) {
            DYCreateSignatureTextCellModel *model = data[@"data"];
            _model = model;
            _titleLabel.text = model.title;
            _textField.text = model.content;
            _textField.placeholder = model.tip;
            _textField.keyboardType = UIKeyboardTypeDefault;
            
            if ([model.title isEqualToString:@"题目"]) {
                _myButton.hidden = YES;
            } else {
                _myButton.hidden = NO;
            }
        }
    }
}

- (IBAction)textFieldChange:(UITextField *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(createSignatureTextCell:textField:)]) {
        [_delegate createSignatureTextCell:self textField:sender];
    }
}

- (IBAction)textFieldBegin:(UITextField *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(createSignatureTextCell:textField:)]) {
        [_delegate createSignatureTextCell:self textField:sender];
    }
}

- (IBAction)buttonAction:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(createSignatureTextCell:buttonAction:)]) {
        [_delegate createSignatureTextCell:self buttonAction:sender];
    }
}

@end


#pragma mark - Model

@implementation DYCreateSignatureTextCellModel

+ (DYCreateSignatureTextCellModel *)ittemModelWithTitle:(NSString *)title content:(NSString *)content tip:(NSString *)tip index:(NSInteger)index {
    DYCreateSignatureTextCellModel *model = [DYCreateSignatureTextCellModel new];
    model.title = title.length > 0 ? title : @"";
    model.content = content.length > 0 ? content : @"";
    model.tip = tip.length > 0 ? tip : @"";
    model.index = index;
    return model;
}

@end
